package cn.wolfcode.luowowo.packagetour.service.impl;

import cn.wolfcode.luowowo.packagetour.repository.IPackageTourCommentTemplateRepository;
import cn.wolfcode.luowowo.packagetour.service.IPackageTourCommentService;
import cn.wolfcode.luowowo.packagetour.template.PackageTourCommentTemplate;
import com.alibaba.dubbo.config.annotation.Service;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.SearchQuery;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service
public class PackageTourCommentServiceImpl implements IPackageTourCommentService {
    @Autowired
    private IPackageTourCommentTemplateRepository dao;
    @Autowired
    private ElasticsearchTemplate template;
    @Autowired
    private TransportClient client;

    //保存数据
    public void save(PackageTourCommentTemplate packageTourCommentTemplate) {
        dao.save(packageTourCommentTemplate);
    }

   //根据跟团游信息id查询评论
    public List<PackageTourCommentTemplate> searchByPackageTourId(Long packageTourId) {
        Iterable<PackageTourCommentTemplate> iterable = dao.search(QueryBuilders.matchQuery("packageTourId", packageTourId));
        ArrayList<PackageTourCommentTemplate> list = new ArrayList<>();
        Iterator<PackageTourCommentTemplate> iterator = iterable.iterator();
        while (iterator.hasNext()){
            list.add(iterator.next());
        }
        return list;
    }

    //搜索指定id的跟团游信息
    public PackageTourCommentTemplate searchById(Long id) {
        Iterable<PackageTourCommentTemplate> iterable = dao.search(QueryBuilders.matchQuery("id", id));
        Iterator<PackageTourCommentTemplate> iterator = iterable.iterator();
        if (iterator.hasNext()){
            return iterator.next();
        }
        return new PackageTourCommentTemplate();
    }

    //创建索引
    public void createIndex() {
        template.createIndex(PackageTourCommentTemplate.class);
        template.getMapping(PackageTourCommentTemplate.class);
    }
}
